package com.xc.stock.domain.basic.repository;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ag.utils.SysConstant.ProgressEnum;
import com.rp.spring.jpa.GenericJpaRepository;
import com.xc.stock.domain.basic.convert.AccountActionConvert;
import com.xc.stock.domain.basic.convert.IEntityPOConvert;
import com.xc.stock.domain.basic.entity.AccountAction;
import com.xc.stock.infrastructure.db.po.AccountActionPO;
import com.xc.stock.infrastructure.db.po.QAccountActionPO;
import com.xc.stock.infrastructure.db.repo.AccountActionRepo;
import com.xc.stock.utils.SpringUtils;

import lombok.RequiredArgsConstructor;

/**
 * AccountActionRepository
 *
 * @author xlfd
 * @email xlfd@gmail.com
 * @created 2022/03/25
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountActionRepository extends SimplePoConvertEntityRepository<AccountAction, AccountActionPO, Integer> {
	final AccountActionRepo repo;
	final AccountActionConvert convert;
	static final QAccountActionPO q = QAccountActionPO.accountActionPO;

	@Override
	public GenericJpaRepository<AccountActionPO, Integer> repo() {
		return repo;
	}

	@Override
	public IEntityPOConvert<AccountAction, AccountActionPO> convert() {
		return convert;
	}

	public static AccountActionRepository of() {
		return SpringUtils.getBean(AccountActionRepository.class);
	}

	public boolean limitWithrawPass(AccountAction accountAction) {
		accountAction.limitWithrawPass();
		
		return getJPAQueryFactory()
					.update(q)
					.set(q.limitWithdrawStatus, ProgressEnum.SUCCESS.v())
					.set(q.ext, accountAction.getExt())
					.where(q.id.eq(accountAction.getId()), q.limitWithdrawStatus.eq(ProgressEnum.DOING.v()))
					.execute() > 0
			;
		
	}

	public List<AccountAction> findNeedLimitWithrawPass() {
		Date curr = new Date();
		return findAll(q.limitWithdrawStatus.eq(ProgressEnum.DOING.v()).and(q.limitWithdrawAt.isNotNull()).and(q.limitWithdrawAt.before(curr)));
	}
}
